package com.atguigu.maven.controller;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.atguigu.maven.bean.Department;
import com.atguigu.maven.bean.Employee;
import com.atguigu.maven.serverce.DepartmentServerce;
import com.atguigu.maven.serverce.EmployeeServerce;

@Controller
public class EmployeeController {
	@Autowired
	private EmployeeServerce employeeservice;
	@Autowired
	private DepartmentServerce departmentservice;
	
	//显示所有员工信息
	@RequestMapping(value="/emp",method=RequestMethod.GET)
	public String getAllEmps(Map<String,Object> map) {
		List<Employee> list = employeeservice.getAllEmps();
		map.put("emps", list);
		return "list";
		
	}
	
	//去往增加的页面
	@RequestMapping(value="/update",method=RequestMethod.GET)
	public String toAdd(Map<String,Object> map) {
		//获取所有部门信息
		List<Department> alldept = departmentservice.getAllDepartments();
		map.put("alldept", alldept);
		
		map.put("employee", new Employee());
		return "input";
		
	}
	
	//具体的添加操作
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public String addNewEmp(Employee employee) {
		employeeservice.addEmps(employee);
		return "redirect:/emp";
	}
	
	//去往修改的页面
	@RequestMapping(value="/update/{id}",method=RequestMethod.GET)
	public String toUpdate(@PathVariable("id")Integer id,Map<String,Object> map) {
		List<Department> alldept = departmentservice.getAllDepartments();
		map.put("alldept", alldept);
		
		Employee employee = employeeservice.getEmpsById(id);
		map.put("employee", employee);
		
		return "input";
		
		
	}
	
	//具体的修改
	@RequestMapping(value="/update", method=RequestMethod.PUT)
	public String updateEmp(Employee employee) {
		employeeservice.updateEmps( employee);
		return "redirect:/emp";
		
	}
	//删除员工
	@RequestMapping(value="/delete/{id}")
	@ResponseBody
	public String deleteEmp(@PathVariable("id")Integer id) {
		employeeservice.deleteEmps(id);
		return "1";
	}
	
	
	
	

}
